package java_util.coo_util.Log;

import java_util.interfaceType.I_Log;

import java.text.SimpleDateFormat;
import java.util.Date;

public class C_Console implements I_Log {
    public C_LogTxtSave o_localLog;
    int showLogLevel;
    static Date date;

    public C_Console S_Init(boolean isLocalSave, int showLogLevel, String deviceInfo, String prefixPath) {
        if (isLocalSave) {
            o_localLog = new C_LogTxtSave(deviceInfo, prefixPath);
        }
        this.showLogLevel = showLogLevel;
        C_Log.o_logObj = this;
        //SetConsoleCtrlHandler(d_cancelHandler, true);
        return this;
    }


    @Override
    public void S_Log(int type, String tag, String time, String log, String stackTrace) {
        // TODO 自动生成的方法存根
        if (o_localLog != null) {
            o_localLog.S_Log(type, tag, time, log, stackTrace);
        }
        if (type == E_LogType.e0_Error) {
            S_println(type, log + "\n" + stackTrace);
        } else {
            S_println(type, log);
        }
    }


    static StringBuilder bb = new StringBuilder();
    synchronized public static void S_println(int type, Object... s) {
        bb.delete(0, bb.length());
        for (Object str : s) {
            bb.append(str);
            bb.append(" ");
        }
        if (type == E_LogType.e0_Error) {
            System.err.println(bb.toString());
        } else {
            bb.append(".............");
            bb.append(time());
            bb.append("__");
            // 获取调用的位置
            StackTraceElement[] stack = new Exception().getStackTrace();
            bb.append(stack[4].getClassName().replaceAll("\\$\n.$", ""));
            bb.append("___.(" + stack[4].getFileName() + ":" + stack[4].getLineNumber() + ")");
            System.out.println(bb.toString());
        }
    }

    static String time() {
        date = new Date();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }
}
